﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Common;
using BusinessLayer;
using SeAssignment.Models;

namespace SeAssignment.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult About()
        {
            return View();
        }

        public ActionResult HomePage()
        {
            List<Product> list = new ProductBL().GetAllProducts().ToList();
            return View("HomePage", list);
        }

        public ActionResult MyPurchaseProducts()
        {
            List<PurchaseModel> list = new List<PurchaseModel>();
            foreach (Common.Views.UserItem u in new ProductBL().GetUserProducts(HttpContext.User.Identity.Name))
            {
                PurchaseModel p = new PurchaseModel();
                p.PurchaseId = u.PurchaseID;
                p.ProductID = u.ID;
                p.ProductName = u.ProductName;
                p.ProductImage = new ProductBL().GetProductByID(u.ID).ImageURL;
                p.Price = u.AmountPurchased;
                p.WarrantyDate = u.WarranyExpires.AddYears(2);
                list.Add(p);
            }

            return View("MyPurchaseProducts", list);
        }

        public ActionResult MyOrders()
        {
            return View("MyOrders", new ProductBL().GetAllOrderByUserName(HttpContext.User.Identity.Name));
        }

        public ActionResult Category(int id)
        {
            List<Product> list = new ProductBL().Search(id, "", 0, 0).ToList();
            return View("HomePage", list);
        }

        public ActionResult SortByHighest()
        {
            List<Common.Product> list = new ProductBL().GetAllProducts().OrderByDescending(p => p.Price).ToList();
            return View("HomePage", list);
        }

        public ActionResult SortByLowest()
        {
            List<Common.Product> list = new ProductBL().GetAllProducts().OrderBy(p => p.Price).ToList();
            return View("HomePage", list);
        }

        public ActionResult SortByDate()
        {
            List<Common.Product> list = new ProductBL().GetAllProducts().OrderByDescending(p => p.DateListed).ToList();
            return View("HomePage", list);
        }
    }
}
